﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Browser;
using VIEWS;
using TZ;

namespace WorldClock
{
    public partial class Page : UserControl
    {
        private VEMap  map;
        private TzData timeZone = null;
        
        public Page()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(Page_Loaded);
        }

        void Page_Loaded(object sender, RoutedEventArgs e)
        {
            map = new VEMap("mapContainer");
            HtmlPage.RegisterScriptableObject("SLMAP", map);
            VELatLong latlong = new VELatLong(180,0);
            map.LoadMap(latlong,2);
            map.MapStyle = VEMapStyle.Hybrid;
            map.Click += new EventHandler<VEMapEventArgs>(map_Click);
            DrawTimes();
        }

        private void map_Click(object sender, VEMapEventArgs e)
        {
            // get the latitude and longitude
            VELatLong latlong = null;

            if (map.MapMode == VEMapMode.Mode3D)
            {
                latlong = e.LatLong; // for 3D mode only
            }
            else
            {
                VEPixel pix = new VEPixel(e.MapX, e.MapY);
                latlong = map.PixelToLatLong(pix);
            }

         //   this.LocalTime.Text = latlong.Latitude.ToString();
            //this.CityTime.Text = latlong.Longitude.ToString();

            this.timeZone = TzData.FindTz(latlong.Latitude, latlong.Longitude);
            this.DrawTimes();

            e.Handled = true;
        }

        private void DrawTimes()
        {
            DateTime now = DateTime.Now.ToLocalTime();
            DateTime utc = now.ToUniversalTime();

            this.LocalTime.Text = now.ToLocalTime().ToShortTimeString();
            this.LocalDate.Text = now.ToLocalTime().ToLongDateString();
            this.UTCTime.Text = now.ToUniversalTime().ToShortTimeString();
            this.UTCDate.Text = now.ToUniversalTime().ToLongDateString();
            
            if (this.timeZone == null)
            {
                this.CityName.Text = "Click on Map";
                this.CityTime.Text = "Click on Map";
                this.CityDate.Text = "Click on Map";
            }
            else
            {
                DateTime cityTime = this.GetCurrentOffset(utc);
                this.CityName.Text = this.timeZone.Location;
                this.CityTime.Text = cityTime.ToShortTimeString();
                this.CityDate.Text = cityTime.ToLongDateString();
            }
        }

        private DateTime GetCurrentOffset(DateTime utc)
        {
            int minOffset;

            if (StartedYet(this.timeZone.Offset2, utc) ||
                !StartedYet(this.timeZone.Offset1, utc))
            {
                minOffset = this.timeZone.Offset2.OffsetMinutes;
            }
            else
            {
                minOffset = this.timeZone.Offset1.OffsetMinutes;
            }

            return utc.AddMinutes(minOffset);
        }

        // TODO: NOT RIGHT -- doesn't convert utc correctly
        // nor does it test the transition time (which isn't even
        // correct in the data)
        //
        // TODO: Might be good to normalize the rules in the data
        // to UTC so we don't have to figure it out here.
        private bool StartedYet(OffsetInfo offset, DateTime utc)
        {
            if (offset.StartMonth < utc.Month)
                return true;

            if (offset.StartMonth > utc.Month)
                return false;

            // harder if we're in the target month
            if (offset.StartDay == -1)
            {
                // TODO: NOT RIGHT -- doesn't convert utc correctly
                // nor does it test the transition time.
                return offset.StartDate <= utc.Day;
            }

            // choices left are Sun>=3 or LastSun type choices
            // TODO: We aren't bothering to fix these right now,
            // our demo isn't happening on that kind of a date.
            return false;
        }
    }
}
